package com.ecar.api.support.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ecar.api.quartz.jobs.SendOrderJob;
import com.ecar.api.service.order.OrderService;
import com.ecar.api.support.dto.Constants;
import com.ecar.api.support.dto.order.DriverWilddogDto;
import com.ecar.common.util.BeanUtils;
import com.ecar.common.util.DataValidateUtil;
import com.ecar.core.order.entity.Order;
import com.wilddog.client.DataSnapshot;
import com.wilddog.client.SyncError;
import com.wilddog.client.SyncReference;
import com.wilddog.client.ValueEventListener;

public class OrderUtil {
	
	private static final Logger log = LoggerFactory.getLogger(SendOrderJob.class);
	
	/**
	 * @author fire
	 * @param orderNum 父订单号
	 * @param origins 司机的实时坐标
	 * @return
	 */
	public static void sortOrder(final List<Order> list){
		
		if(list.size()==0){
			log.info("无订单可排序");
			return;
		}
		log.info("===================开始排序===========================");
		SyncReference driverRef = WilddogUtil.getDriverRef("WilddogLocation/map");
		driverRef.addListenerForSingleValueEvent(new ValueEventListener() {
			@Override
			public void onDataChange(DataSnapshot dataSnapshot) {
				//获取司机的实时定位
				double x=(double) dataSnapshot.child(list.get(0).getDriverId()).child("location").child("0").getValue();
				double y=(double) dataSnapshot.child(list.get(0).getDriverId()).child("location").child("1").getValue();
				//司机的坐标点
				String origins=x + "," + y; 
				//为司机的行程排序
				log.info("为司机的行程排序");
				String ordernn="";
				double minDistance=-1;
				for(Order carpool:list){
					double tempDistance=0.0;
					//获取订单状态到达上车点（4）和开始行程（5）时的终点位置
					if(carpool.getOrderStatus()==Constants.ARRIVE_POINT || carpool.getOrderStatus()== Constants.START_ROAD) {
						String desLocation=carpool.getDesLocation();
						desLocation=desLocation.replace(":", ",");
						tempDistance=GaoDeMapUtil.getDistance(origins, desLocation);
						if(tempDistance==-1){
							log.info("行程查询失败");
							continue;
						}
					}else if(carpool.getOrderStatus()==Constants.ALREADY_GET_ORDER){
						//获取订单状态为司机已接单（3）的起点坐标
						String startLocation=carpool.getStartLocation();
						startLocation=startLocation.replace(":", ",");
						tempDistance=GaoDeMapUtil.getDistance(origins, startLocation);
						if(tempDistance==-1){
							log.info("行程查询失败");
							continue;
						}
					}
					if(minDistance<=0){
						minDistance=tempDistance;
						ordernn=carpool.getOrderNum();
					}else if(minDistance>=tempDistance){
						minDistance=tempDistance;
						ordernn=carpool.getOrderNum();
					}
				}
				HashMap<String, Object> resultMap=new HashMap<String, Object>();
				resultMap.put("carpool", ordernn);
				WilddogUtil.sendSortDriver(list,list.get(0).getDriverId(), resultMap);
				log.info("排序成功");
				//WilddogUtil.sendMessage(WilddogUtil.getDriverRef("WilddogLocation/order"), list.get(0).getDriverId()+"/now", resultMap);
			}
			@Override
			public void onCancelled(SyncError arg0) {
			}
		});
	}
/*	private static void sort(List<Map.Entry<String, Double>> list){
		Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {  
			//降序排序  
			@Override  
			public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {  
				//return o1.getValue().compareTo(o2.getValue());  
				return o2.getValue().compareTo(o1.getValue());  
			}  
		});  
	}*/
}
